<?php

use App\Resource\ApiResource;
use Hyperf\Utils\ApplicationContext;
use Psr\Container\ContainerInterface;


/**
 * 获取容器
 * @return \Psr\Container\ContainerInterface
 */
function container(): ContainerInterface
{
    return ApplicationContext::getContainer();
}

/**
 * 接口返回结果
 * @param string $message 提示信息
 * @param null $data 额外数据
 * @param int $code 状态码
 * @return array|\Psr\Http\Message\ResponseInterface
 */
function json(string $message = null, $data = null, int $code = 200)
{
    return (new ApiResource($data, $message, $code))->with();
}

/**
 * @param Throwable $exception
 * @return array|\Psr\Http\Message\ResponseInterface
 */
function errorJson(Throwable $exception)
{
    return (new ApiResource($exception))->with();
}